import { controller, get, post, required } from '../decorator/router'
const Wechat = require('wechat-jssdk');

const wechatConfig = {
  appId: 'wx915174011f1a8231',
  appSecret: 'eea16beff0b0d3e8a8bf673b8ae74ee0',
  wechatToken: 'Zth4dLD8d9xlVsdE_sarine_token',
}

const wx = new Wechat(wechatConfig)

@controller('')
class WechatController {

  @get('/api/wechat')
  async wechatConfig(ctx, next) {
    console.log(ctx.query)
    const data = wx.jssdk.verifySignature(ctx.query)
    console.log('====================================');
    console.log(data);
    console.log(ctx.query);
    console.log(ctx.query.echostr);
    console.log('====================================');
    if (data) {
      // ctx.body = {
      //   errorCode: 200,
      //   data: data.echostr,
      //   msg: '获取微信配置成功'
      // }
      ctx.body = ctx.query.echostr
    } else {
      ctx.body = {
        errorCode: 500,
        data: '',
        msg: '获取微信配置失败'
      }
    }
  }

  @get('/get-signature')
  async wechatSignature(ctx, next) {
    console.log('====================================');
    console.log(ctx.query.url);
    console.log('====================================');
    try {
      const signatureDate = await wx.jssdk.getSignature(ctx.query.url)
      ctx.body = { 
        errorCode: 200,
        data: signatureDate,
        msg: '获取微信配置成功'
      }
    } catch (error) {
      ctx.body = {
        errorCode: 500,
        data: error,
        msg: '获取微信签名失败'
      }
    }
  }

}

export default WechatController